sCore = {
    _classIDCounter:1,
    cnd:"$", //control name delimiter
    parentClass : 'parentClass',
    classID : 'classID',
    makeClass : function (a, b, d) {
        var c = function() {
            $(this.init.apply(this,arguments));
        },e='prototype',f,ag;
        if ($.isFunction(a)) {
            (f = function() { })[e] = a[e];
            ((c[e] = new f()).constructor = c)[this.parentClass] = a[e];
            a = b;
        }
        c[this.classID] = this._classIDCounter++;
        $.extend(c[e],a);
        if (d) $.extend(c,d);
        return c;
    },
    callDOMElementMethod : function(id,method,args) {
        var c = window[id].$;
        c[method].apply(c,[args]);
    },
    newEvent : function(n) {
        return new jQuery.Event(n);
    },
    initChildInstances : function(o,cc) {
        var i,c,cn='class',cs={};
        for (i in o[cc]) {
            $.extend(cs[i] = new window[(c=o[cc][i])[cn]](c),{
                name:i,
                parent:o
            });
            delete cs[i][cn];
        }
        o[cc] = cs;
    },
    isSubclassOf : function(o,f) {
        var r = false,c = o.constructor,fc = SCArrayHelper.flip($.isArray(f) ? SCLinqHelper.select(f,this.classID):[f[this.classID]]);
        while (c!=null) {
            if (c[this.classID] in fc) return true;
            if (c = c[this.parentClass]) c = c.constructor;
        }
        return false;
    }
};
SCArrayHelper = {
    flip : function ( trans ) {
        var k, tm = {};
        for ( k in trans )
            if ( trans.hasOwnProperty(k) )
                tm[trans[k]] = k;
        return tm;
    },
    filter : function(a,f) {
        var r = [],i,e;
        for (i in a) if(f.call(e = a[i])) r.push(e);
        return r;
    }
};
SCLinqHelper = {
    select : function (a,f) {
        var r = [],i;
        for (i in a) r.push(a[i][f]);
        return r;
    },
    selectFrom : function(a,c,k,d,r) {
        var ai,r = !r?(k?{}:[]):r,i,isf = $.isFunction(k);
        for (i in a[c]) {
            ai=a[c][i];
            k?r[isf?k.call(ai):ai[k]]=ai:r.push(ai);
            if (d && ai[c]) SCLinqHelper.selectFrom(ai,c,k,d,r);
        }
        return r;
    }
}
SCStruct = sCore.makeClass({
    init : function (args) {
        $.extend(this,args);
    },
    isSubclassOf : function(f) {
        return sCore.isSubclassOf(this, f);
    }
});
SCEventFirer = sCore.makeClass(SCStruct,{
    init : function() {
        SCEventFirer.parentClass.init.apply(this,arguments);
        this.__$ = $(this);
        //for (var i in {bind:1,each:1,trigger:1,on:1}) this[i] = jQuery.fn[i];
    },
    bind : function() {
        this.__$.bind.apply(this.__$,arguments);
        return this;
    },
    trigger : function() {
        this.__$.trigger.apply(this.__$,arguments);
        return this;
    }
});

SCAjaxRequest = sCore.makeClass(SCEventFirer,{
    EVENT_REQUEST_START : 'req_start',
    EVENT_REQUEST_SUCCESS : 'req_success',
    EVENT_REQUEST_COMPLETE : 'req_complete',
    EVENT_REQUEST_ERROR : 'req_error',
    init : function() {
        SCAjaxRequest.parentClass.init.apply(this,arguments);
        this.rid = (new Date).getTime()+Math.floor(Math.random()*10000000)
    },
    Send : function(args) {
        this.trigger(this.EVENT_REQUEST_START);
        return $.ajax({
            type: "POST",
            url: this.url,
            data: $.extend(args,{
                rid:this.rid
            }),
            dataType: "html",
            success:$.proxy(this.success,this)
        });
    },
    FrameForm_onSuccess : function (e) {
        $(e.currentTarget).unbind();
        this.complete();
        this.success();
    },
    OnStart : function(l) {
        return this.bind(this.EVENT_REQUEST_START,l);
    },
    OnSuccess : function(l) {
        return this.bind(this.EVENT_REQUEST_SUCCESS,l);
    },
    OnComplete : function(l) {
        return this.bind(this.EVENT_REQUEST_COMPLETE,l);
    },
    OnError : function(l) {
        return this.bind(this.EVENT_REQUEST_ERROR,l);
    },
    complete: function() {
        this.trigger(this.EVENT_REQUEST_COMPLETE);
    },
    success: function(msg){
        this.response = msg;
        this.trigger(this.EVENT_REQUEST_SUCCESS,this);
    },
    error: function(msg, status, error) {
        $.extend(this,{
            response:msg,
            status:status,
            error:error
        });
        this.trigger(this.EVENT_REQUEST_ERROR);
    }
});